﻿using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;

namespace HYRElectric.PowerSystem
{
	public class ExtensibleStorageManager
	{
		public static void WriteExtensibleStorageToElement(ref ViewDrafting element, string schemaName, string fieldName)
		{
			Guid guid = new Guid(ExtensibleStorageManager.m_GUID);
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				SchemaBuilder schemaBuilder = new SchemaBuilder(guid);
				schemaBuilder.SetSchemaName(schemaName);
				schemaBuilder.AddSimpleField(fieldName, typeof(string));
				schema = schemaBuilder.Finish();
			}
			Entity entity = new Entity(schema);
			Field field = schema.GetField(fieldName);
			entity.Set<string>(field, element.Name);
			element.SetEntity(entity);
		}

		public static bool ReadExtensibleStorageFromElement(Element element)
		{
			Guid guid = new Guid(ExtensibleStorageManager.m_GUID);
			Schema schema = Schema.Lookup(guid);
			Entity entity = element.GetEntity(schema);
			return entity.Schema != null && entity.Schema.GUID.ToString() == guid.ToString();
		}

		private static string m_GUID = "267A4ABF-A6A3-4C23-BD9D-33C29B12CA67";
	}
}
